var flowApi = require('../../script/flow.js');
var app = getApp();
var config = require('../../script/config.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    userinfo: null,
    InsName: '',
    input_timeOut: null,
    ActList: [],
    PageInfo: {
      Sum: 0,
      PageSize: 15,
      CurrPage: 1,
      Sort: ""
    },
    canLoadData: true,
      noData: false,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    wx.preloadWebview?.();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.getUserInfo()
    this._refreshData();
    this.getActList(true)
  },
  _refreshData: function () {
    this.setData({ //初始化数据
      InsName: '',
      input_timeOut: null,
      PageInfo: {
        Sum: 0,
        PageSize: 15,
        CurrPage: 1,
        Sort: ""
      },
      canLoadData: true,
      noData: false
    })
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {
    wx.showNavigationBarLoading();
    this._refreshData();
    this.getActList(true)
    wx.hideNavigationBarLoading();
    wx.stopPullDownRefresh();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    if (this.data.canLoadData) {
      this.setData({
        'PageInfo.CurrPage': this.data.PageInfo.CurrPage + 1
      })
      this.getActList();
    } else {
      wx.showToast({
        title: '没有更多数据了',
        icon: 'none',
        duration: 1000
      });
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },
  getUserInfo: function () {
    try {
      var value = wx.getStorageSync(config.storageKey.userinfo)
      if (value) {
        this.setData({
          userinfo: value
        })
      }
    } catch (e) {
      // Do something when catch error
    }
  },
  inputChange(e) {
    this._refreshData();
    let { input_timeOut } = this.data
    if (input_timeOut) {
      clearTimeout(input_timeOut)
    }
    input_timeOut = setTimeout(() => {
      this.setData({
        InsName: e.detail.value
      })
      this.getActList(true)
    }, 300);
    this.setData({
      input_timeOut
    })
  },
  getActList(isRefresh){
    wx.showLoading({
      title: '加载中',
      mask: true
    })
    let params = {
      SystemMark: 'XtYthHeat',
      accessToken: wx.getStorageSync(config.storageKey.token),
      PageInfo: this.data.PageInfo,
      Entity:{
        GroupeCode:"",
        StaCreateTime:"",
        EndCreateTime:"",
        FinishStaTime:"",
        FinishEndTime:"",
        FlowId:null,
        ACTTYPE:"",
        OrgNo:"",
        ActState:null,
        NodeId:null,
        InsName: this.data.InsName,
        FlowRemarks:null
      }
    };
    flowApi.GetHandlePage(params).then(res => {
      if (res.Result === 1) {
        let dataList = []
        if (isRefresh) {
          dataList = [...res.Datas]
        } else {
          dataList = [...this.data.ActList, ...res.Datas]
        }
        let canLoadData = res.PageInfo.PageSize * res.PageInfo.CurrPage <= res.PageInfo.Sum
        const noData = dataList.length === 0
        this.setData({
          ActList: dataList,
          canLoadData: canLoadData,
          noData: noData,
          PageInfo: res.PageInfo
        })
      }
      wx.hideLoading();
    }).catch(_err => {
      wx.hideLoading();
    });
  },
  toDetail: function (e) {
    const item = e.currentTarget.dataset.item
    // http://36.99.164.197:8260/vue_bpm/#/wfms/run/InsActManage/ActInfoPage?accessToken=f05d07cd-3532-41c0-8eaf-4f26a7237509&FlowId=3&NodeId=5&PageType=1&InsId=&ActId=
    const url = "../actinfo/actinfo?PageType=2&InsId=" + item.InsId + "&ActId=" + item.ActId;
    wx.navigateTo({
      url: url
    })
  },
  ToDo: function (e) {
    const item = e.currentTarget.dataset.item
    const url = "../actinfo/actinfo?PageType=1&InsId=" + item.InsId + "&ActId=" + item.ActId;
    wx.navigateTo({
      url: url
    })
  },
  Receive: function (e) {
    const that = this;
    const item = e.currentTarget.dataset.item
    wx.showLoading({
      title: '接收中',
      mask: true
    });
    let params = {
      SystemMark: 'XtYthHeat',
      accessToken: wx.getStorageSync(config.storageKey.token),
      PageInfo: this.data.PageInfo,
      Entity:{
        ActId: item.ActId,
        ActOperInfo: ""
      }
    };
    flowApi.Receive(params).then(res => {
      if (res.Result === 1) {
        wx.showToast({
          title: res.Msg,
          icon: 'success',
          duration: 4000,
          success: function () {
            that._refreshData();
            that.getActList(true);
          }
        })
      } else {
        wx.showToast({
          title: res.Msg,
          icon: 'error',
          duration: 2000
        })
      }
    }).catch(_err => {
      wx.hideLoading();
    });
  }
})